package org.example.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.springcloud.entities.Payment;
import org.example.springcloud.service.PaymentService;
import org.example.springcloud.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @author Jandmla
 * Date: 2022/7/22
 * Time: 14:15
 * Description:
 */
@RestController
@Slf4j
public class PaymentControllerImpl implements PaymentController{

    private final PaymentService paymentService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    public PaymentControllerImpl(PaymentService paymentService){
        this.paymentService = paymentService;
    }

    /**
     * 测试轮训，返回端口号识别
     * @return 端口号信息
     */
    @GetMapping(value = "/payment/lb")
    public CommonResult<String> getPaymentLB(){
        return new CommonResult<String>(200,"查询成功！",serverPort);
    }

    /**
     * 数据插入
     * @param payment 数据实体
     * @return 插入结果
     */
    @PostMapping(value = "/payment/create")
    public CommonResult<Integer> create(@RequestBody Payment payment){
        int insert = paymentService.create(payment);
        log.info("/payment/create    \t  create(Payment payment) ====> result = {}",insert);
        if (insert > 0){
            return new CommonResult<>(200,"数据插入成功！  serverPort:  "+ serverPort,insert);
        }else {
            return new CommonResult<>(500,"操作失败！  serverPort:  "+ serverPort);
        }
    }

    /**
     * 数据查询
     * @param id ID
     * @return 查询结果
     */
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> get(@PathVariable("id") Long id){
        Payment result = paymentService.getPaymentById(id);
        log.info("/payment/get       \t  get(Long id) ====> result = {}",result.toString());
        if (null != result.getId()){
            return new CommonResult<>(200,"数据查询成功！  serverPort:  "+ serverPort,result);
        }else {
            return new CommonResult<>(450,"数据查询失败，可能没有该数据！  serverPort:  "+ serverPort);
        }
    }

    @GetMapping(value = "/payment/discovery/8001")
    public CommonResult<DiscoveryClient> discovery(){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        instances.forEach(inst -> {
            log.info("getServiceId: {} \t getHost: {} \t getInstanceId: {} \t getUri: {} \t getScheme: {}",
                    inst.getServiceId(),
                    inst.getHost(),
                    inst.getInstanceId(),
                    inst.getUri(),
                    inst.getScheme());
        });
        return new CommonResult<>(200,"操作成功",discoveryClient);
    }

    /**
     * 超时测试
     * @return 端口号
     */
    @GetMapping(value = "/payment/geign/timeout")
    public String paymentFeignTimeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return serverPort;
    }
}